#include "widget.h"
#include "ui_widget.h"

#include <QPainter>
#include <qtimer.h>
#include <QtMath>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{

    ui->setupUi(this);
    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,[=](){
        if(roatFlag ==0){
            offsetCount++;
            if(offsetCount > 60){
               roatFlag=1;
            }
        }

        if(roatFlag == 1){
            offsetCount --;
            if(offsetCount <= 0){
               roatFlag = 0;
            }
        }

        update();
    });
    timer->setInterval(20);
    timer->start();
}



Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *event)
{
   QPainter mPainter(this);
    mPainter.setRenderHint(QPainter::Antialiasing,true);
   mPainter.setBrush(Qt::black);
   mPainter.drawRect(rect());
   QPoint cent(rect().width()/2, rect().height()*0.6);
   mPainter.translate(cent);

//   QRadialGradient radialGradient(0, 0, height()/2);
//   radialGradient.setColorAt(0.0, QColor(255,0,0,50));
//   // 中心颜色
//   radialGradient.setColorAt(1.0, QColor(255,0,0,250)); // 外围颜色
//   mPainter.setBrush(QBrush(radialGradient));
//   //大圈
   mPainter.drawEllipse(QPoint(0,0),height()/2,height()/2);
   mPainter.setBrush(Qt::NoBrush);
   mPainter.setPen(QPen(Qt::white,3));
   mPainter.drawEllipse(QPoint(0,0),50,50);
   //刻度
   double angle = 240 * 1.0 / 60;
   //保存原始坐标系
   mPainter.save();
   mPainter.rotate(150);
   mPainter.setFont(QFont("华文宋体",15));
   for(int i= 0;i<=60;i++){
       if(i>=40){
           mPainter.setPen(QPen(Qt::red,3));
       }
       if(i % 5 == 0){
          mPainter.drawLine(height()/2 -20,0,height()/2 -2,0);
       }else{
          mPainter.drawLine(height()/2 -8,0,height()/2 -2,0);
       }
       mPainter.rotate(angle);

   }
   mPainter.restore();
   mPainter.setPen(Qt::white);
   mPainter.setFont(QFont("华文宋体",18));
   int r = height() /2 -40;
   for (int i=0;i<= 60;i++) {
       if(i % 5 == 0){
           //保存坐标系
           mPainter.save();
           //算出平移点，弧度=角度*3.1415/180
           int delX = qCos((210-angle*i)*M_PI/180) * r;
           //QT中sin认的是弧度
           int delY = qSin(qDegreesToRadians(210-angle*i)) * r;
           //平移坐标系
           mPainter.translate(QPoint(delX,-delY));
           //旋转坐标系
           mPainter.rotate(-120+angle*i);
           //angle=4,30*4=120的时候，实参是0，120 //写上文字
           mPainter.drawText(-25,-25,50,30,Qt::AlignCenter,QString::number(i*4));
           //恢复坐标系
           mPainter.restore();
       }
   }

   //指针
   mPainter.save();
   mPainter.setBrush(Qt::white);
   mPainter.setPen(Qt::NoPen);
   static const QPointF points[3] = {
       QPointF(0,0),
       QPointF(170.0,0),
       QPointF(0,10),
   };
   mPainter.rotate(150 + offsetCount*angle);
   mPainter.drawPolygon(points, 4);
   mPainter.drawLine(50,0,height()/2 -50,0);

   //扇形区域
   mPainter.restore();
   QRect rect(-height()/2,-height()/2 ,height() ,height());
   mPainter.setPen(Qt::NoPen);
   mPainter.setBrush(QBrush(QColor(255,0,0,100)));
   mPainter.drawPie(rect,(360-149)*16,-angle*offsetCount*16);//负数逆时针画扇形

   //内圈发光
   QRadialGradient radialGradient(0,0,100);
   radialGradient.setColorAt(0.0, QColor(255,0,0,200)); // 中心颜色
   radialGradient.setColorAt(1.0, QColor(0,0,0,100)); // 外围颜色
   mPainter.setBrush(radialGradient);
   mPainter.drawEllipse(QPoint(0,0),100,100);

   //内部黑色圆
   mPainter.setBrush(Qt::black);
   mPainter.drawEllipse(QPoint(0,0),80,80);

   //外部发光圈
   int rr = height()/2 + 15;
   QRect rentangle(-rr,-rr,rr*2,rr*2);
   mPainter.setPen(Qt::NoPen);
   QRadialGradient radiaGradient(0,0,rr);
   radiaGradient.setColorAt(1,QColor(255,0,0,200));
   radiaGradient.setColorAt(0.97,QColor(255,0,0,120));
   radiaGradient.setColorAt(0.9,QColor(0,0,0,0));
   radiaGradient.setColorAt(0,QColor(0,0,0,0));
   mPainter.setBrush(radiaGradient);
   mPainter.drawPie(rentangle,(360-150)*16,-angle*61*16);

   //当前速度
   mPainter.setPen(Qt::white);
   QFont font("Arial",30);
   font.setBold(true);
   mPainter.setFont(font);
   mPainter.drawText(QRect(-60,-60,120,70),Qt::AlignCenter,QString::number(offsetCount*4));
   QFont font2("Arial",13); font.setBold(true);
   mPainter.setFont(font2);
   mPainter.drawText(QRect(-60,-60,120,160),Qt::AlignCenter,"Km/h");


   //Logo
   QRect rec(-100,height()/2 * 0.5,200,60);
   mPainter.drawPixmap(rec,QPixmap(":/lufu2.png"));
}




